package com.tiantianyuegou.activity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.MyAdapter;
import com.tiantianyuegou.util.Content;
import com.tiantianyuegou.util.PinyinComparator;
import com.tiantianyuegou.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 地址拼音界面
 */
public class CityListPin extends AppCompatActivity {
    private ListView mListView;
    private SideBar indexBar;
    private WindowManager mWindowManager;
    private TextView mDialogText;
//    private View head;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_list_pin);
        mListView = (ListView) this.findViewById(R.id.list);
        indexBar = (SideBar) findViewById(R.id.sideBar);
        mDialogText = (TextView) LayoutInflater.from(this).inflate(R.layout.list_position, null);
//        head = LayoutInflater.from(this).inflate(R.layout.head, null);
//        mListView.addHeaderView(head);
        mDialogText.setVisibility(View.INVISIBLE);
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mWindowManager.addView(mDialogText, lp);
        indexBar.setTextView(mDialogText);
        //初始化数据
        List<Content> list = new ArrayList<Content>();
        for (int i = 0; i < 10; i++) {
            Content m;
            if (i < 3)
                m = new Content("A", "选项" + i);
            else if (i < 6)
                m = new Content("F", "选项" + i);
            else
                m = new Content("D", "选项" + i);
            list.add(m);
        }
        //根据a-z进行排序
        Collections.sort(list, new PinyinComparator());
        // 实例化自定义内容适配类
        MyAdapter adapter = new MyAdapter(this, list);
        // 为listView设置适配
        mListView.setAdapter(adapter);
        //设置SideBar的ListView内容实现点击a-z中任意一个进行定位
        indexBar.setListView(mListView);
    }
}
